import constants as con
import random
import pygame

class Goal(pygame.sprite.Sprite):
    image = None

    def __init__(self,parent,str,duration):
        pygame.sprite.Sprite.__init__(self)

        self.parent = parent
        self.sprites = pygame.sprite.LayeredUpdates()

        self.angle = random.randint(-20,20)
        self.duration = duration

        left = int(con.window_size[0] * 0.078125)
        right = int(con.window_size[0] * 0.921875)
        self.x = random.randint(left, right)

        top = int(con.window_size[1] * 0.209)
        bottom = int(con.window_size[1] * 0.317)
        self.y = random.randint(top,bottom)

        if Goal.image is None:
            Goal.image = con.load_image('dat_burst2.png')


        self.str = parent.font.render(str, True, (255,0,0))
        self.width = self.str.get_width()
        self.height = self.str.get_height()

        self.image = pygame.transform.smoothscale(Goal.image,(int(self.width * 1.2),int(self.height * 1.9)))
        self.image.blit(self.str, ((self.width * 0.1),(self.height * 0.6)))

        self.image = pygame.transform.rotate(self.image, self.angle)

        self.rect = pygame.Rect(0,0,self.width,self.height)
        self.rect.center = (self.x,self.y)

        self.sprites.add(self)

        self.age = 0

    def purge(self):
        self.sprites.remove(self)
        self.parent.goals.remove(self)

    def redraw(self):
        self.sprites.draw(self.parent.screen)
        #self.parent.screen.blit(self.str, (self.x,self.y))

        if self.parent.thump:
            self.age += 1

        if self.age > self.duration:
            self.purge()


